#!/usr/bin/perl
use strict;
use FindBin;
use Cwd qw(abs_path);
use Getopt::Long;
use JSON;
use WebCtl;

sub usage {
    my $pname = $FindBin::Script;
    print("$pname --targetstatus <up|down> --retrycount <retry count> --timeout <timeout seconds> <url1> <url2>\n");
    exit(-1);
}

sub main {
    $| = 1;    #不对输出进行buffer，便于实时看到输出日志

    my $targetStatus = 'up';
    my $retryCount   = 1;
    my $timeout      = 10;

    GetOptions(
        'targetstatus=s' => \$targetStatus,
        'retrycount=i'   => \$retryCount,
        'timeout=i'      => \$timeout
    );
    my @requests = @ARGV;

    my $webCtl = WebCtl->new();

    my $isFailed = 1;

    if ( $targetStatus eq 'up' ) {
        for ( my $i = 1 ; $i <= $retryCount ; $i++ ) {
            print("INFO: Begin url check...\n");
            my $startTime = time();
            eval {
                foreach my $req (@requests) {
                    my @reqInfo = split( /\s*,\s*/, $req );
                    my $method  = uc( $reqInfo[0] );
                    my $url     = $reqInfo[1];
                    my $data    = $reqInfo[2];

                    print("INFO: Http request $url.\n");
                    if ( $method eq 'POST' ) {
                        my $jsonData;
                        eval { $jsonData = from_json($data); };
                        if ( defined($jsonData) ) {
                            $webCtl->doRest( 'POST', $url, $jsonData );
                        }
                        else {
                            $webCtl->doPost( $url, $data );
                        }
                    }
                    elsif ( $method eq 'GET' ) {
                        $webCtl->get($url);
                    }
                    else {
                        my $jsonData;
                        if ( defined($data) and $data ne '' ) {
                            $jsonData = from_json($data);
                        }
                        $webCtl->doRest( $method, $url, $jsonData );
                    }
                    print("INFO: Http request succeed.\n");
                }
            };
            if ($@) {
                my $errMsg = $@;
                $errMsg =~ s/ at\s*.*$//;
                print("WARN: $errMsg");
                my $leftTime = $timeout - ( time() - $startTime );
                if ( $leftTime > 0 ) {
                    sleep($leftTime);
                }
            }
            else {
                $isFailed = 0;
                print("INFO: Http requests all complete succeed.\n");
                last;
            }
        }

        if ( $isFailed == 0 ) {
            print("FINE: Wait url up succeed.\n");
        }
        else {
            print("ERROR: Wait url up failed(timeout).\n");
        }
    }
    else {
        for ( my $i = 1 ; $i <= $retryCount ; $i++ ) {
            print("INFO: Begin url check...\n");
            my $startTime = time();
            eval {
                foreach my $req (@requests) {
                    my @reqInfo = split( /\s*,\s*/, $req );
                    my $method  = uc( $reqInfo[0] );
                    my $url     = $reqInfo[1];
                    my $data    = $reqInfo[2];

                    print("INFO: Http request $url.\n");
                    if ( $method eq 'POST' ) {
                        my $jsonData;
                        eval { $jsonData = from_json($data); };
                        if ( defined($jsonData) ) {
                            $webCtl->doRest( 'POST', $url, $jsonData );
                        }
                        else {
                            $webCtl->doPost( $url, $data );
                        }
                    }
                    elsif ( $method eq 'GET' ) {
                        $webCtl->get($url);
                    }
                    else {
                        my $jsonData;
                        if ( defined($data) and $data ne '' ) {
                            $jsonData = from_json($data);
                        }
                        $webCtl->doRest( $method, $url, $jsonData );
                    }
                    print("INFO: Http request succeed.\n");
                }
            };
            if ($@) {
                my $errMsg = $@;
                $errMsg =~ s/ at\s*.*$//;
                print("INFO: $errMsg");
                $isFailed = 0;
                last;
            }
            else {
                print("INFO: Http requests all complete succeed.\n");
                my $leftTime = $timeout - ( time() - $startTime );
                if ( $leftTime > 0 ) {
                    sleep($leftTime);
                }
            }
        }

        if ( $isFailed == 0 ) {
            print("FINE: Wait url down succeed.\n");
        }
        else {
            print("ERROR: Wait url down failed(timeout).\n");
        }
    }

    return $isFailed;
}

exit main();
